//
//  TCLYSubmitAddressViewController.m
//  YuErmuYing
//
//  Created by LYang on 16/9/3.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "TCLYSubmitAddressViewController.h"
#import "TCLYGoodsAdressTableViewCell.h"
#import "TCLYHomeModel.h"
#import "Masonry.h"
#import "Macros.h"
#import "TCLYUser.h"
#import "TCLYAddressViewController.h"

@interface TCLYSubmitAddressViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) TCLYHomeModel * homeModel;
@property (nonatomic, strong) UITableView * addressTableView;
@end

@implementation TCLYSubmitAddressViewController

- (void)viewDidLoad {
    [self createTitleView];
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadIntegralAddress) name:@"loadIntegralAddress" object:nil];
    self.homeModel = [[TCLYHomeModel alloc] init];
    TCLYUser * user = [[TCLYUser alloc] init];
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:user.userId, @"user_id", nil];
    [self.homeModel loadIntegralAddressParameter:dic];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mineAddressNo) name:@"mineAddressNo" object:nil];
    [self createView];
}
-(void)loadIntegralAddress{
    [self.addressTableView reloadData];
}
-(void)createTitleView{
    self.navigationController.navigationBar.barStyle = UIBarMetricsCompactPrompt;
    self.navigationController.navigationBar.backgroundColor = statusColor;
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, -20, screenWidth, 20)];
    view.backgroundColor = navColor;
    [self.navigationController.navigationBar addSubview:view];
    
    UILabel * titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"选择收货地址";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.frame = CGRectMake(0, 0, 50, 20);
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = titleLabel;
    
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(12, 22, 20, 20);
    [leftBtn setImage:[UIImage imageNamed:@"left"] forState:UIControlStateNormal];
    leftBtn.tag = 91;
    [leftBtn addTarget:self action:@selector(statusBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    rightBtn.tag = 92;
    [rightBtn setTitle:@"管理" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    rightBtn.frame = CGRectMake(screenWidth-12-20, 22, 24, 24);
    [rightBtn addTarget:self action:@selector(statusBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}
-(void)statusBtnAction:(id)sender{
    switch (((UIButton *)sender).tag) {
        case 91:{
            [self.navigationController popViewControllerAnimated:YES];
            self.tabBarController.tabBar.hidden = YES;
        }
            break;
        case 92:{
            TCLYAddressViewController * addressViewCtr = [[TCLYAddressViewController alloc] init];
            addressViewCtr.isMineAddress = NO;
            [self.navigationController pushViewController:addressViewCtr animated:YES];
        }
            break;
    }
}
-(void) createView{
    self.addressTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
    self.addressTableView.backgroundColor = bgColor;
    [self.addressTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.addressTableView.delegate = self;
    self.addressTableView.dataSource = self;
    [self.view addSubview:self.addressTableView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.homeModel.integralAddressArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * identifier = @"goodsAddressCell";
    TCLYGoodsAdressTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[TCLYGoodsAdressTableViewCell alloc] initWithReuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.nameLabel.text = [NSString stringWithFormat:@"收货人: %@",self.homeModel.integralAddressArr[indexPath.row][@"address_consignee"]];
    cell.telLabel.text =  self.homeModel.integralAddressArr[indexPath.row][@"address_consignee_phone"];
    cell.addressLabel.text = [NSString stringWithFormat:@"收货地址: %@%@",self.homeModel.integralAddressArr[indexPath.row][@"address_add"],self.homeModel.integralAddressArr[indexPath.row][@"address_detail"]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100;
}
//点击cell触发的代理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.navigationController popViewControllerAnimated:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"updateSucceed" object:self userInfo:self.homeModel.integralAddressArr[indexPath.row]];
}
-(void)mineAddressNo{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadIntegralAddress) name:@"loadIntegralAddress" object:nil];
    TCLYUser * user = [[TCLYUser alloc] init];
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:user.userId, @"user_id", nil];
    [self.homeModel loadIntegralAddressParameter:dic];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
